//
//  RoomCommentsViewController.m
//  YJRenter
//
//  Created by 文彬 on 2018/7/12.
//  Copyright © 2018年 文彬. All rights reserved.
//

#import "RoomCommentsViewController.h"
#import "CommentUserInfoTableViewCell.h"
#import "RoomCommentHeaderView.h"
#import "CommentContentTableViewCell.h"
#import "CommentPictureTableViewCell.h"
#import "CommentFeedBackTableViewCell.h"

@interface RoomCommentsViewController ()<UITableViewDelegate,
    UITableViewDataSource>

@property (strong, nonatomic) UITableView *listTableView;
@property (strong, nonatomic) NSArray *comments;
@property (strong, nonatomic) NSArray *feedBacks;
@property (strong, nonatomic) NSMutableArray *feedBackHeights;

//内容高度及是否展开 数组单项@{@"cellHeight":xx,@"showAll":xx,@"contentHeight":xx} showAll: -1不显示全文按钮 0显示全文按钮 1显示收起按钮
@property (strong, nonatomic) NSMutableArray *contentAtributions;

@end

@implementation RoomCommentsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"全部评价";
    self.comments = @[@"房间很干净，很不错房间很干净，很不错房间很干净，很不错房间很干净，很不错房间很干净，很不错房间很干净，很不错房间很干净，很不错",
                      @"房间很干净，很不错房间很干净，很不错房间很干净，很不错房间很干净，很不错房间很干净，很不错房间很干净，很不错",
                      @"房间很干净，很不错房间很干净，很不错房间很干净，很不错房间很干净，很不错房间很干净，很不错房间很干净，很不错房间很干净，很不错房间很干净，很不错房间很干净，很不错房间很干净，很不错房间很干净，很不错",
                      @"房间很干净，很不错房间很干净，很不错房间很干净，很不错房间很干净，很不错房间很干净，很不错房间很干净，很不错房间很干净，很不错房间很干净，很不错",
                      @"房间很干净，很不错房间很干净，很不错房间很干净，很不错"];
    self.feedBacks = @[@"房东回复：感谢您的好评，祝你好心情",@"",@"房东回复：感谢您的好评，祝你好心情感谢您的好评，祝你好心情：感谢您的好评，祝你好心情",@"",@"房东回复：感谢您的好评，祝你好心情感谢您的好评，祝你好心情感谢您的好评，祝你好心情"];
    
    self.feedBackHeights = [[NSMutableArray alloc]init];
    [self handleCommentData];
    
    [self initPageControl];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear: animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark 页面相关
- (void)initPageControl{
    
    [self.view addSubview:self.listTableView];
    [self.listTableView makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.equalTo(@0);
    }];
    
    [self addTableViewHeader];
}

- (void)addTableViewHeader{
    
    RoomCommentHeaderView *header = [[RoomCommentHeaderView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 270)];
    self.listTableView.tableHeaderView = header;
}

- (void)handleCommentData{
    
    for (NSString *conetnt in self.comments) {
        
        CGFloat height = [StaticTools getLabelHeight:conetnt defautWidth:SCREEN_WIDTH-50 font:[UIFont systemFontOfSize:15] lineSpace:5];
        if (height<42) { //少于或等于两行 不需要显示全文按钮
            //cell高度需要加上下边距距离
            NSMutableDictionary *dict = [[NSMutableDictionary alloc]initWithDictionary:
                                         @{@"contentHeight":@(height),
                                           @"showAll":@(-1),
                                           @"cellHeight":@(height+10+40)}];
            [self.contentAtributions addObject:dict];
        }
        else{
            NSMutableDictionary *dict = [[NSMutableDictionary alloc]initWithDictionary:
                                         @{@"contentHeight":@(height),
                                           @"showAll":@(0),
                                           @"cellHeight":@(42+10+40)}];
            [self.contentAtributions addObject:dict];
        }
    }
    
    for (NSString *string in self.feedBacks) {
        if ([StaticTools isEmptyString:string]) {
            [self.feedBackHeights addObject:@(0)];
        }
        else{
            CGFloat height = [StaticTools getLabelHeight:string defautWidth:SCREEN_WIDTH-60 font:[UIFont systemFontOfSize:15] lineSpace:5];

            [self.feedBackHeights addObject:@(height)];
        }
    }
}

#pragma mark UITableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.comments.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 4;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return section==0?10:0.1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row==0) {
        return 60;
    }
    else if(indexPath.row==1){
        NSDictionary *info = self.contentAtributions[indexPath.section];
        if ([info[@"showAll"] intValue] ==-1||
            [info[@"showAll"] intValue] ==0) {
            return [info[@"cellHeight"] floatValue];
        }
        else if ([info[@"showAll"] intValue] ==1) {
            return [info[@"contentHeight"] floatValue]+10+40;
        }
    }
    else if(indexPath.row==2){
        return 100;
    }
    
    CGFloat height = [self.feedBackHeights[indexPath.section] floatValue];
    if (height==0) {
        return 0;
    }
    return height+20+10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
 
    if (indexPath.row==0) { //评论者信息
        CommentUserInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CommentUserInfoTableViewCell" forIndexPath:indexPath];
        [cell.imgView setRoundImageWithURL:@"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=4036889747,3526425271&fm=27&gp=0.jpg" placeholderImage:nil];
        cell.nameLabel.text = @"张三";
        return cell;
    }
    else if(indexPath.row==1){ //评论内容
        
        CommentContentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CommentContentTableViewCell" forIndexPath:indexPath];
        cell.contentLabel.text = self.comments[indexPath.section];
        [UILabel changeLineSpaceForLabel:cell.contentLabel WithSpace:5];
        
        NSMutableDictionary *info = self.contentAtributions[indexPath.section];
        [cell.allBtn buttonClick:^{
            if ([info[@"showAll"] intValue] ==0) {
                info[@"showAll"] = @(1);
            }
            else if ([info[@"showAll"] intValue] ==1) {
                info[@"showAll"] = @(0);
            }
            [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        }];
        
      
        if ([info[@"showAll"] intValue] ==-1) {
            cell.allBtn.hidden = YES;
        }
        else if ([info[@"showAll"] intValue] ==0) {
            cell.allBtn.hidden = NO;
            [cell.allBtn setTitle:@"全文" forState:UIControlStateNormal];
        }
        else if ([info[@"showAll"] intValue] ==1) {
            cell.allBtn.hidden = NO;
            [cell.allBtn setTitle:@"收起" forState:UIControlStateNormal];
        }
        
        return cell;
    }
    else if(indexPath.row==2){ //评论图片
        
        CommentPictureTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CommentPictureTableViewCell" forIndexPath:indexPath];
        for (int i=0; i<3; i++) {
            UIImageView *imgView = [cell.contentView viewWithTag:i+100];
            [imgView sd_setImageWithURL:[NSURL URLWithString:@"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=4036889747,3526425271&fm=27&gp=0.jpg"] placeholderImage:nil];
        }
        return cell;
    }
    
    //房东反馈
    CommentFeedBackTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CommentFeedBackTableViewCell" forIndexPath:indexPath];
    cell.contentLabel.text = self.feedBacks[indexPath.section];
    [UILabel changeLineSpaceForLabel:cell.contentLabel WithSpace:5];
    return cell;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

#pragma mark getter
- (UITableView*)listTableView{
    if (_listTableView==nil) {
        _listTableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _listTableView.delegate = self;
        _listTableView.dataSource = self;
        _listTableView.sectionFooterHeight = 0;
        _listTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _listTableView.backgroundColor = [UIColor clearColor];
        [_listTableView registerNib:[UINib nibWithNibName:@"CommentUserInfoTableViewCell" bundle:nil] forCellReuseIdentifier:@"CommentUserInfoTableViewCell"];
        [_listTableView registerNib:[UINib nibWithNibName:@"CommentContentTableViewCell" bundle:nil] forCellReuseIdentifier:@"CommentContentTableViewCell"];
        [_listTableView registerNib:[UINib nibWithNibName:@"CommentPictureTableViewCell" bundle:nil] forCellReuseIdentifier:@"CommentPictureTableViewCell"];
        [_listTableView registerNib:[UINib nibWithNibName:@"CommentFeedBackTableViewCell" bundle:nil] forCellReuseIdentifier:@"CommentFeedBackTableViewCell"];
        
    }
    
    return _listTableView;
}

- (NSMutableArray*)contentAtributions{
    if (_contentAtributions==nil) {
        _contentAtributions = [[NSMutableArray alloc]init];
    }
    return _contentAtributions;
}

@end
